package org.kzwl.service.app.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.kzwl.comm.advice.BizException;
import org.kzwl.comm.pojo.bo.agree.AgreeBO;
import org.kzwl.entity.app.Agree;
import org.kzwl.mapper.app.AgreeMapper;
import org.kzwl.service.app.AgreeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 张国伟/郭家恒
 * @since 2020-09-10
 */
@Service
public class AgreeServiceImpl extends ServiceImpl<AgreeMapper, Agree> implements AgreeService {

    @Resource
    private AgreeMapper agreeMapper;

    //region 点赞
    @Override
    public void addAgree(AgreeBO bo) {
        QueryWrapper<Agree> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Agree::getCuAgreeExt,bo.getCuAgreeExt()).eq(Agree::getCuUserId,bo.getCuUserId());
        Integer integer = agreeMapper.selectCount(queryWrapper);
        if (integer>0){
            throw new BizException("不可重复点赞");
        }
        Agree agree = new Agree();
        BeanUtils.copyProperties(bo,agree);
        agreeMapper.insert(agree);
    }
    //endregion

    //region 取消点赞
    @Override
    public void removeAgree(AgreeBO bo) {
        QueryWrapper<Agree> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Agree::getCuAgreeExt,bo.getCuAgreeExt()).eq(Agree::getCuUserId,bo.getCuUserId());
        Integer integer = agreeMapper.selectCount(queryWrapper);
        if (integer<=0){
            throw new BizException("未点赞不可取消");
        }
        agreeMapper.delete(queryWrapper);

    }
    //endregion
}
